/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							 script_adjust_hero_attribute.cpp

	$Header: /heroes4/script_adjust_hero_attribute.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "script_adjust_hero_attribute.h"

#include "adv_object_type.h"
#include "army.h"
#include "basic_dialog.h"
#include "hero.h"
#include "script_targeting.h"
#include "streambuf_operators.h"

static bool g_modification_applied;

// --------------------------------------------------------------------------
// t_script_adjust_hero_attribute members
// --------------------------------------------------------------------------

bool t_script_adjust_hero_attribute::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	if ( !t_script_hero_adjustment_action< t_uint16 >::read_from_map( stream, format_version ) )
		return false;

	m_adjustment *= m_direction;
	m_permanent = get< t_uint8 >( stream ) != 0;
	
	return true;
}

bool t_script_adjust_hero_attribute::read( std::streambuf& stream, int version )
{
	if ( !t_script_hero_adjustment_action< t_uint16 >::read( stream, version ) )
		return false;

	m_adjustment = get< t_int8 >( stream );
	m_permanent = get< bool >( stream );

	return true;
}

bool t_script_adjust_hero_attribute::write( std::streambuf& stream ) const
{
	if ( !t_script_hero_adjustment_action< t_uint16 >::write( stream ) )
		return false;

	put< t_int8 >(stream, m_adjustment);
	put< bool >(stream, m_permanent);

	return true;
}


void t_script_adjust_hero_attribute::make_adjustment(t_adventure_map* map, t_hero* hero) const
{
	if (hero == NULL) {
		g_modification_applied = false;
		return;
	} else 
		g_modification_applied = true;

	if (m_permanent) 
		hero->add_bonus(m_modified_attrib, get_adjustment());
	else 
		hero->add_temp_bonus(m_modified_attrib, get_adjustment());
}

void t_script_hero_increase_defense::add_icons(t_basic_dialog* dialog) const
{
	if (g_modification_applied)
		dialog->add_defense(get_adjustment());
}

void t_script_hero_decrease_defense::add_icons(t_basic_dialog* dialog) const
{
	if (g_modification_applied)	
		dialog->add_defense(get_adjustment());
}

void t_script_hero_increase_speed::add_icons(t_basic_dialog* dialog) const
{
	if (g_modification_applied)
		dialog->add_speed(get_adjustment());
}

void t_script_hero_decrease_speed::add_icons(t_basic_dialog* dialog) const
{
	if (g_modification_applied)
		dialog->add_speed(get_adjustment());
}

void t_script_hero_increase_max_spell_points::add_icons(t_basic_dialog* dialog) const
{
	if (g_modification_applied)
		dialog->add_spell_points(get_adjustment());
}

void t_script_hero_decrease_max_spell_points::add_icons(t_basic_dialog* dialog) const
{
	if (g_modification_applied)
		dialog->add_spell_points(get_adjustment());
}

void t_script_adjust_hero_attack::add_icons(t_basic_dialog* dialog) const
{
	if (g_modification_applied)
		dialog->add_attack( get_adjustment() );
}

